package com.vf.admin.miaodong.controller;


import com.vf.admin.client.service.AppUserService;
import com.vf.admin.common.BaseController;
import com.vf.admin.common.entity.Result;
import com.vf.admin.miaodong.entity.AppSetup;
import com.vf.admin.miaodong.service.AfterRoomService;
import com.vf.admin.miaodong.service.AfterSetupService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 后台管理协议管理
 */
@CrossOrigin
@Controller
@RequestMapping("/after/setup")
public class AfterSetUpController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    AfterSetupService afterSetupService;
    @Autowired
    AfterRoomService afterRoomService;
    @Autowired
    AppUserService appUserService;
    @Autowired
    private AfterGiftController afterGiftController;

    /**
     * 获取参数配置详情
     *
     * @Param: []
     * @return: java.lang.String
     * @Author: zhouyunyi
     * @Date: 2021/07/4
     * @Time: 11:22
     */
    @RequestMapping("/list")
    public String canshulist() {
        try {
            AppSetup byId = afterSetupService.get();
            request.setAttribute("byId", byId);
            return this.sucess("/web/after/setup/list");
        } catch (Exception e) {
            logger.error("查看参数配置信息异常", e);
            return this.error("系统繁忙");
        }
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Result saveOrUpdate(AppSetup appSetup)
    {
        appSetup.setId(1);
        afterSetupService.save(appSetup);
        return this.sucessJSON("保存成功");
    }


    /**
     * 更改参数配置信息
     *
     * @Param: []
     * @return: com.vf.admin.common.entity.Result
     * @Author: zhouyunyi
     * @Date: 2021/07/4
     * @Time: 11:32
     */
    /*@RequestMapping("/saveOrUpdate123")
    @ResponseBody
    public Result saveOrUpdate123(double rechargeratio, double withdrawalratio, Integer renzhengfanli,
                               Integer dashanmoney, String dashanimg, String dashantext, double guizufanli, Integer fensituanmoney, Integer videomoney, Integer videofanli,
                               Integer qingganroom, Integer daysmsnum, Integer daysmsmoney, Integer fensituanfanli, BigDecimal qiandaojiangli, Integer yinpinmoney, Integer yinpinfanli,
                               String ossurl, Integer liveManage, Integer withdrawal,Integer giftSharing,Double jinDouExchangeForDiamonds,Integer ipnumber,Long videoMin,Long wihdrawalMin,Long wihdrawalMax,
                               Integer createFamilyForDiamonds,Integer quitFamilyForDay,Integer punchInFamilyWard,Integer punchInFamilyPrestige,
                               Integer songliFamilyPrestige,Integer shouliFamilyPrestige,String priceSetExplain,Integer faceCompScore,BigDecimal intimacyGuard,
                               BigDecimal intimacyZuanshi,BigDecimal guardZeng,Integer eachMessageCharmGrowth,String diamondsToCharmGrowth,
                               Integer voiceSignatureParseGrowth,Integer dynamicParseEveryDayMax,Integer dynamicParseCharmGrowth,Integer voiceAndVideoEveryMinuteCharmGrowth,
                               BigDecimal decIntimacyForHours,BigDecimal decIntimacyForNum,BigDecimal Maxintimacy,Integer rechargeInvite,Integer giftInvite) {
        try {
            if (StringEx.isNull(rechargeratio)) {
                return this.errorJSON("充值比例不能为空");
            }
            if (StringEx.isNull(withdrawalratio)) {
                return this.errorJSON("提现抽成比例不能为空");
            }
            if (StringEx.isNull(guizufanli)) {
                return this.errorJSON("开通贵族给上级返利比例不能为空");
            }
            if (StringEx.isNull(renzhengfanli)) {
                return this.errorJSON("用户认证上级返利金币不能为空");
            }
            if (StringEx.isNull(dashanmoney)) {
                return this.errorJSON("搭讪成功奖励金币不能为空");
            }
            if (StringEx.isNull(dashanimg)) {
                return this.errorJSON("搭讪礼物图片地址不能为空");
            }
            if (StringEx.isNull(dashantext)) {
                return this.errorJSON("搭讪语不能为空");
            }
            if (StringEx.isNull(videomoney)) {
                return this.errorJSON("1V1视频聊天每分钟扣费钻石不能为空");
            }
            if (videomoney < 0) {
                return this.errorJSON("1V1视频聊天每分钟扣费钻石不能小于0");
            }
            if (StringEx.isNull(videofanli)) {
                return this.errorJSON("1V1视频聊天主播每分钟返利不能为空");
            }
            if (videofanli < 0) {
                return this.errorJSON("1V1视频视频聊天每分钟返利不能小于0");
            }
            if (StringEx.isNull(yinpinmoney)) {
                return this.errorJSON("1V1音频聊天每分钟扣费钻石不能为空");
            }
            if (yinpinmoney < 0) {
                return this.errorJSON("1V1音频聊天每分钟扣费钻石不能小于0");
            }
            if (StringEx.isNull(yinpinfanli)) {
                return this.errorJSON("1V1音频聊天主播每分钟返利不能为空");
            }
            if (yinpinfanli < 0) {
                return this.errorJSON("1V1音频聊天主播每分钟返利不能小于0");
            }
            if (StringEx.isNull(qiandaojiangli)) {
                return this.errorJSON("签到奖励不能为空");
            }
            if (StringEx.isNull(withdrawal)) {
                return this.errorJSON("提现配置不能为空");
            }
            if (StringEx.isNull(videoMin)) {
                return this.errorJSON("1V1音视频最低持有钻石不能为空");
            }

            if (StringEx.isNull(wihdrawalMin)) {
                return this.errorJSON("提现最少数量不能为空");
            }

            if (StringEx.isNull(wihdrawalMax)) {
                return this.errorJSON("提现最大数量不能为空");
            }

            if(StringEx.isNull(createFamilyForDiamonds)){
                return this.errorJSON("创建家族消耗钻石数量不能为空");
            }

            if(StringEx.isNull(quitFamilyForDay)){
                return this.errorJSON("退出家族不可再加入家族时间天数限制不能为空");
            }

            if(StringEx.isNull(punchInFamilyWard)){
                return this.errorJSON("家族签到奖励金币数量不能为空");
            }

            AppSetup setup = afterSetupService.get();
            setup.setRenzhengfanli(renzhengfanli);
            setup.setRechargeratio(rechargeratio);
            setup.setWithdrawalratio(withdrawalratio);
            setup.setDashanmoney(dashanmoney);
            setup.setDashanimg(dashanimg);
            setup.setDashantext(dashantext);
            setup.setGuizufanli(guizufanli);
            setup.setFensituanmoney(fensituanmoney);
            setup.setFensituanfanli(fensituanfanli);
            setup.setDaysmsnum(daysmsnum);
            setup.setDaysmsmoney(daysmsmoney);
            setup.setQingganroom(qingganroom);
            setup.setQiandaojiangli(qiandaojiangli);
            setup.setVideomoney(videomoney);
            setup.setVideofanli(videofanli);
            setup.setYinpinmoney(yinpinmoney);
            setup.setYinpinfanli(yinpinfanli);
            setup.setOssurl(ossurl);
            setup.setLiveManage(liveManage);
            setup.setWithdrawal(withdrawal);
            setup.setGiftSharing(giftSharing);
            setup.setJinDouExchangeForDiamonds(jinDouExchangeForDiamonds);
            setup.setIpnumber(ipnumber);
            setup.setIpnumber(ipnumber);
            setup.setVideoMin(videoMin);
            setup.setWihdrawalMin(wihdrawalMin);
            setup.setWihdrawalMax(wihdrawalMax);
            //////////////////
            setup.setCreateFamilyForDiamonds(createFamilyForDiamonds);
            setup.setQuitFamilyForDay(quitFamilyForDay);
            setup.setPunchInFamilyWard(punchInFamilyWard);
            setup.setPunchInFamilyPrestige(punchInFamilyPrestige);
            setup.setSongliFamilyPrestige(songliFamilyPrestige);
            setup.setShouliFamilyPrestige(shouliFamilyPrestige);
            setup.setPriceSetExplain(priceSetExplain);
            setup.setFaceCompScore(faceCompScore);
            setup.setIntimacyGuard(intimacyGuard);
            setup.setIntimacyZuanshi(intimacyZuanshi);
            setup.setGuardZeng(guardZeng);
            setup.setEachMessageCharmGrowth(eachMessageCharmGrowth);
            setup.setDiamondsToCharmGrowth(diamondsToCharmGrowth);
            setup.setVoiceSignatureParseGrowth(voiceSignatureParseGrowth);
            setup.setDynamicParseCharmGrowth(dynamicParseCharmGrowth);
            setup.setDynamicParseEveryDayMax(dynamicParseEveryDayMax);
            setup.setVoiceAndVideoEveryMinuteCharmGrowth(voiceAndVideoEveryMinuteCharmGrowth);
            setup.setDecIntimacyForHours(decIntimacyForHours);
            setup.setDecIntimacyForNum(decIntimacyForNum);
            setup.setMaxintimacy(Maxintimacy);
            setup.setRechargeInvite(rechargeInvite);
            setup.setGiftInvite(giftInvite);
            afterSetupService.save(setup);
            return this.sucessJSON("保存成功");
        } catch (Exception e) {
            logger.error("保存参数配置异常", e);
            return this.errorJSON("系统繁忙");
        }
    }*/

    /**
     * 获取配置详情
     */
    @RequestMapping(value = "edit")
    public String edit() {
        try {
            AppSetup byId = afterSetupService.get();
            request.setAttribute("byId", byId);
            return sucess("/web/after/setup/edit");
        } catch (Exception e) {
            return this.error("系统异常");
        }
    }

    @RequestMapping("/save")
    @ResponseBody
    public Result save(AppSetup appSetup)
    {
        try {
            appSetup.setId(1);
            afterSetupService.save(appSetup);
            return sucessJSON("修改成功");
        }catch (Exception e){
            return errorJSON("网络出现异常，请稍后再试");
        }
    }


    @RequestMapping("/save123")
    @ResponseBody
    public Result save123(String useragreement, String houseagreement, String lianxi, String invitationrulesL, String privacyprotocol, String rechargeagreement,
                       String exchangeagreement, String anchorprotocol, String dengjishuoming, String shimingrenzheng,
                       String shipinrenzheng, String huodongguize, String fensituanshuoming) {
        try {
            if (StringUtils.isEmpty(useragreement)) {
                return errorJSON("用户协议不能为空");
            }
            if (StringUtils.isEmpty(houseagreement)) {
                return errorJSON("房主协议不能为空");
            }
            if (StringUtils.isEmpty(privacyprotocol)) {
                return errorJSON("隐私协议不能为空");
            }
            if (StringUtils.isEmpty(rechargeagreement)) {
                return errorJSON("充值协议不能为空");
            }
            if (StringUtils.isEmpty(exchangeagreement)) {
                return errorJSON("兑换协议不能为空");
            }
            if (StringUtils.isEmpty(anchorprotocol)) {
                return errorJSON("主播协议不能为空");
            }
            if (StringUtils.isEmpty(dengjishuoming)) {
                return errorJSON("等级说明不能为空");
            }
            if (StringUtils.isEmpty(shimingrenzheng)) {
                return errorJSON("实名认证说明不能为空");
            }
            if (StringUtils.isEmpty(shipinrenzheng)) {
                return errorJSON("视频认证说明不能为空");
            }
            if (StringUtils.isEmpty(huodongguize)) {
                return this.errorJSON("活动规则不能为空");
            }
            if (StringUtils.isEmpty(invitationrulesL)) {
                return this.errorJSON("邀请规则不能为空");
            }
            if (StringUtils.isEmpty(lianxi)) {
                return this.errorJSON("联系方式不能为空");
            }
            AppSetup byId = afterSetupService.get();
            byId.setUseragreement(useragreement);
            byId.setHouseagreement(houseagreement);
            byId.setPrivacyprotocol(privacyprotocol);
            byId.setRechargeagreement(rechargeagreement);
            byId.setExchangeagreement(exchangeagreement);
            byId.setAnchorprotocol(anchorprotocol);
            byId.setDengjishuoming(dengjishuoming);
            byId.setShimingrenzheng(shimingrenzheng);
            byId.setShipinrenzheng(shipinrenzheng);
            byId.setLianxi(lianxi);
            byId.setHuodongguize(huodongguize);
            byId.setInvitationrulesL(invitationrulesL);
            byId.setFensituanshuoming(fensituanshuoming);
            afterSetupService.save(byId);
            return sucessJSON("修改成功");
        } catch (Exception e) {
            return errorJSON("网络出现异常，请稍后再试");
        }
    }

    @RequestMapping(value = "/setupUpload")
    @ResponseBody
    public Map<String, Object> setupUpload(MultipartFile myFileName) throws Exception {
        Map<String, Object> map = new HashMap<>(16);
        //img的路径
        String idPicPath = null;
        if (!myFileName.isEmpty()) {
            //控制文件的大小
            if (myFileName.getSize() <= 1073741824) {
                /*Result result = afterGiftController.uploadLiveImg(myFileName);
                Map<String,Object> imageMap = JSONObject.parseObject(JSON.toJSONString(result.getData()));
                idPicPath = imageMap.get("name").toString();*/

                idPicPath = appUserService.aliyunOssupload(myFileName);
            }
        }
        List list = new ArrayList();
        list.add(idPicPath);
        map.put("errno", 0);
        map.put("data", list);
        return map;
    }
}
